package com.pengxun.manager.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.pengxun.manager.dao.system.PermissionApiDao;
import com.pengxun.manager.model.system.PermissionApi;
import com.pengxun.manager.model.system.UserRole;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.PermissionApiService;
import com.pengxun.manager.vo.AssignPermissionApiVO;
import com.pengxun.manager.vo.PermissionApiVO;
import com.szeastroc.common.jdbc.page.Page;


@Service
public class PermissionApiServiceImpl implements PermissionApiService{

	@Resource
	private PermissionApiDao permissionApiDao;
	
	@Resource
    private LoginService loginService;
	
	@Override
	public int insert(PermissionApi permissionApi) {
		return permissionApiDao.insert(permissionApi);
	}
	
    @Override
    public int update(PermissionApi permissionApi){
        return permissionApiDao.update(permissionApi);
    }
   	
    @Override
    public int delete(Integer id){
       return permissionApiDao.delete(id);
 	}
   
    @Override
    public Page findByPage(PermissionApiVO permissionApiVO){
    	 List<PermissionApi> permissionApis = permissionApiDao.findByPage(permissionApiVO);
    	 Page page = new Page();
    	 BeanUtils.copyProperties(permissionApiVO, page);
    	 page.setData(permissionApis);
         return page;
    }
    
    public PermissionApi findById(Integer id){
          return permissionApiDao.findById(id);
    }

	@Override
	public List<PermissionApi> findByPermissionCode(String permissionCode) {
		return permissionApiDao.findByPermissionCode(permissionCode);
	}

	@Override
	public int batchInsert(List<PermissionApi> list) {
		return permissionApiDao.batchInsert(list);
	}

	@Override
	public int assignPermissionApi(AssignPermissionApiVO assignPermissionApiVO) {
		List<PermissionApi> list = new ArrayList<PermissionApi>();
	    Date date = new Date();

       Userinfo currentUser = loginService.getCurrentUser();

       for (String apiPath:assignPermissionApiVO.getApiPaths()){
    	   PermissionApi permissionApi = new PermissionApi();
    	   permissionApi.setPermissionCode(assignPermissionApiVO.getPermissionCode());
    	   permissionApi.setApiPath(apiPath);
    	   permissionApi.setCreateDate(date);
    	   permissionApi.setCreateBy(currentUser.getUserId());

	        list.add(permissionApi);
        }
       permissionApiDao.deleteByPermissionCode(assignPermissionApiVO.getPermissionCode());
       return permissionApiDao.batchInsert(list);
	}

	
}
